/*
 * vite配置
 *
 * @Author:    1024创新实验室-主任：卓大
 * @Date:      2022-05-02 23:44:56
 * @Wechat:    zhuda1024
 * @Email:     lab1024@163.com
 * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
 */
import path from 'path';
import vue from '@vitejs/plugin-vue';
import customVariables from './src/theme/custom-variables';
import { createSvg } from './src/components/SvgLoad/index'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { presetUno, presetAttributify, presetIcons, presetMini } from 'unocss'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
const pathResolve = path.resolve(__dirname, 'src')

export default {


  optimizeDeps: {
    // 开发时 解决这些commonjs包转成esm包
    include: [
      "@jiaminghi/c-render",
      "@jiaminghi/c-render/lib/plugin/util",
      "@jiaminghi/charts/lib/util/index",
      "@jiaminghi/charts/lib/util",
      "@jiaminghi/charts/lib/extend/index",
      "@jiaminghi/charts",
      "@jiaminghi/color",
    ],
  },
  build: {
    // 打包时需要另外处理的commonjs规范的包
    commonjsOptions: {
      include: [/node_modules/]
    },
  },
  base: process.env.NODE_ENV === 'production' ? '/' : '/',
  root: process.cwd(),
  resolve: {
    alias: [
      // 国际化替换
      {
        find: 'vue-i18n',
        replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
      },
      // 绝对路径重命名：/@/xxxx => src/xxxx
      {
        find: /\/@\//,
        replacement: path.resolve('src') + '/',
      },
      {
        find: /^~/,
        replacement: '',
      },
    ],
  },
  // 服务端渲染
  server: {
    proxy: {
      '/api': {
        target: 'http://10.171.7.100:8090',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/mapi/excel': 'http://10.171.7.100:80',
      '/mapi': {
        target: 'http://10.171.7.138:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mapi/, '')
      },
      '/eapi': {
        target: 'http://10.171.7.135:8080/coldbigdata',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/eapi/, '')
      },
      '/lapi': {
        target: 'http://10.171.7.135:8080/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/lapi/, '')
      },
      '/hmi': {
        target: 'http://10.171.7.122:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/hmi/, '')
      },
      '/lhmi': {
        target: 'http://10.171.7.123:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/lhmi/, '')
      },
      '/test': {
        target: 'http://10.171.7.122:8091',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/test/, '')
      },
      '/mongodb': {
        target: 'http://10.171.7.136:8888',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mongodb/, '')
      },
      '/CAL1630': {
        target: 'http://10.171.7.123:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/CAL1630/, '')
      },
      '/zinc': {
        target: 'http://10.171.7.122:8888/ben_cbd',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/zinc/, '')
      },
      '/lhm': {
        target: 'http://10.171.7.125:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/lhm/, '')
      },'/mmapi': 'http://localhost:8081',
    },
  },
  plugins: [vue(),
  mockDevServerPlugin({
    include: 'mock/**/*.mock.{ts,js,cjs,mjs,json,json5}',
  }),
  AutoImport({
    imports: ['vue'],
    resolvers: [ElementPlusResolver(),
    IconsResolver({
      prefix: 'Icon',
    })],
    dts: path.resolve(pathResolve, 'auto-imports.d.ts'),
  }),
  Components({
    resolvers: [ElementPlusResolver(),
    IconsResolver({
      enabledCollections: ['ep'],
    })],
    dts: path.resolve(pathResolve, 'components.d.ts'),
  }),
  Icons({
    autoInstall: true,
  }), createSvg("./src/assets/svgs/")],
  build: {
    // 清除console和debugger
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        //配置这个是让不同类型文件放在不同文件夹，不会显得太乱
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: '[ext]/[name]-[hash].[ext]',
        manualChunks(id) {
          //静态资源分拆打包
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString();
          }
        },
      },
    },
    target: 'modules',
    outDir: 'dist', // 指定输出路径
    assetsDir: 'assets', // 指定生成静态文件目录
    assetsInlineLimit: '4096', // 小于此阈值的导入或引用资源将内联为 base64 编码
    chunkSizeWarningLimit: 500, // chunk 大小警告的限制
    minify: 'terser', // 混淆器，terser构建后文件体积更小
    emptyOutDir: true, //打包前先清空原有打包文件
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: customVariables,
        javascriptEnabled: true,
      },
    },
  },
  define: {
    __INTLIFY_PROD_DEVTOOLS__: false,
    'process.env': process.env,
  },
};
